package hard.区间问题.无重叠区间;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 无重叠区间
 * @date 2022/11/22 12:10
 */
public class Demo435 {
    public int eraseOverlapIntervals(int[][] intervals) {
         if(intervals.length<=1){
             return 0;
         }
          Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[0]==o2[0]){
                    return o1[1]-o2[1];
                }
              return  o1[0]-o2[0];
            }
        });
         int count=0;
        for (int i = 1; i < intervals.length; i++) {
            if(intervals[i][0]<intervals[i-1][1]){
                count++;
                intervals[i][1] =Math.min(intervals[i-1][1],intervals[i][1]);
            }
        }
        return count;
    }
}
